package com.yunyike.wx.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.jboss.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yunyike.wx.common.User;
import com.yunyike.wx.common.WeChatException;
import com.yunyike.wx.model.UserInfo;
import com.yunyike.wx.service.IWxUserService;

/**
 * 用户管理接口实现类
 * 
 * @author wenjian
 *
 */
@Service
@Transactional
public class WxUserServiceImp implements IWxUserService{
	
	private final Logger log = LoggerFactory.getLogger(getClass());

	/**
	 * 获取openid
	 */
	public List<String> getuseropenid() {
		List<String> openids=new ArrayList<String>();
		openids=User.getAll();
		return openids;
	}

	/**
	 * 获取用户信息
	 */
	public UserInfo getUserInfo(String openid) {
		UserInfo user=new UserInfo();
		try {
			user=User.getUserInfo(openid);
		} catch (WeChatException e) {
			log.error("根据openid{} 获取用户信息异常:{}", openid, e);
		}
		return user;
	}

	public UserInfo getUserInfo(HttpServletRequest request) {
		
		HttpSession session = request.getSession();
		
		String openid = (String) session.getAttribute("myOpenid");
		
		log.info("openid:{} in session.",openid);
		
		if(StringUtils.isBlank(openid)){
			return null;
		}
		
		return getUserInfo(openid);
	}
	
}
